
/*
 * 通话结束
 * page通话结束
 */
import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/live/liveEnd.dart';
import 'package:qgchat/common/class/date/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/invitationInfoModel.dart';
import 'package:qgchat/states/states_model/index.dart';

class LiveEnd extends StatelessWidget {
  LiveEnd({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final Map args = ModalRoute.of(context).settings.arguments;
    int inv = args["inv"];
    int anchorId = args["anchorId"];
    String targetName = args["targetName"];
    String targetToken = args["targetToken"];
    String targetHead = args["targetHead"];

    UserState user = state(context, 'user');

    return BlocProvider(
      bloc: LiveEndBloc(inv, targetToken),
      child: _LiveEndBloc(
        inv,
        anchorId,
        targetName,
        targetHead,
        user.info.memberType == 1,
      ),
    );
  }
}

class _LiveEndBloc extends StatefulWidget {
  _LiveEndBloc(
    this.inv,
    this.anchorId,
    this.targetName,
    this.targetHead,
    this.isAnchor, {
    Key key,
  }) : super(key: key);

  final int inv;
  final int anchorId;
  final String targetName;
  final String targetHead;
  final bool isAnchor;

  @override
  __LiveEndBlocState createState() => __LiveEndBlocState();
}

class __LiveEndBlocState extends State<_LiveEndBloc> {
  LiveEndBloc bloc;

  @override
  void initState() {
    super.initState();
    if (mounted && !widget.isAnchor) {
      appraise(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    bloc = BlocProvider.of<LiveEndBloc>(context);

    return Scaffold(
      body: Stack(
        children: <Widget>[
          bg(context),
          frostedGlass,
          content(context),
        ],
      ),
    );
  }

  // 底片
  Widget bg(context) {
    return Container(
      width: w100(context),
      height: h100(context),
      child: CachedNetworkImage(
        imageUrl: '${widget.targetHead}',
        width: px(102),
        height: px(102),
        fit: BoxFit.cover,
        errorWidget: (context, url, error) {
          return Config.errorNetImage;
        },
      ),
    );
  }

  // 毛玻璃层
  Widget frostedGlass = BackdropFilter(
    filter: ImageFilter.blur(sigmaX: 30, sigmaY: 30),
    child: Container(
      color: Colors.white.withAlpha(0),
    ),
  );

  // 页面内容
  Widget content(context) {
    return Container(
      width: w100(context),
      height: h100(context),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: px(70)),
            child: StreamBuilder(
              stream: bloc.videoDataStream,
              initialData: bloc.videoDataDefault,
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                InvitationInfoModel data = snapshot.data;
                return Column(
                  children: <Widget>[
                    // 功能按钮
                    toolbarBuild(),
                    // 头像
                    headBuil(),
                    // 昵称
                    nickNameBuild(),
                    // 通话结束
                    callCloseBuild(),
                    // 通话时长
                    callTimeBuild(data),
                    // 直播收益
                    videoPriceTitleBuild(context),
                    // 礼物和通话费用
                    Container(
                      margin: EdgeInsets.only(top: px(55)),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          // 礼物
                          giftBuild(data),
                          // 视频通话计费
                          videoPriceBuild(data),
                        ],
                      ),
                    )
                  ],
                );
              },
            ),
          ),
          Container(
            width: px(690),
            height: px(112),
            margin: EdgeInsets.only(bottom: px(96)),
            child: FlatButton(
              onPressed: () => goHome(context),
              color: col("6f53ff"),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(
                  sp(28),
                ),
              ),
              child: Text(
                "返回首页",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: px(40),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  // 功能按钮
  Widget toolbarBuild() {
    return Container(
      child: Row(
        children: <Widget>[
          Container(
            child: FlatButton(
              padding: EdgeInsets.zero,
              onPressed: () {
                showCupertinoModalPopup<int>(
                  context: context,
                  builder: (context) {
                    return CupertinoActionSheet(
                      message: Text(
                        '请选择举报类型',
                        style: TextStyle(
                          color: col("606266"),
                          fontSize: px(26),
                        ),
                      ),
                      cancelButton: CupertinoActionSheetAction(
                        onPressed: () => Navigator.pop(context),
                        child: Text(
                          "取消",
                          style: TextStyle(
                            color: col("157efb"),
                            fontSize: px(40),
                          ),
                        ),
                      ),
                      actions: <Widget>[
                        CupertinoActionSheetAction(
                          onPressed: () {
                            bloc.reportAnthor(1);
                            Navigator.pop(context, 1);
                          },
                          child: Text(
                            '政治敏感',
                            style: TextStyle(
                              color: col("303133"),
                              fontSize: px(40),
                            ),
                          ),
                        ),
                        CupertinoActionSheetAction(
                          onPressed: () {
                            bloc.reportAnthor(2);
                            Navigator.pop(context, 2);
                          },
                          child: Text(
                            '色情低俗',
                            style: TextStyle(
                              color: col("303133"),
                              fontSize: px(40),
                            ),
                          ),
                        ),
                        CupertinoActionSheetAction(
                          onPressed: () {
                            bloc.reportAnthor(3);
                            Navigator.pop(context, 3);
                          },
                          child: Text(
                            '广告骚扰',
                            style: TextStyle(
                              color: col("303133"),
                              fontSize: px(40),
                            ),
                          ),
                        ),
                        CupertinoActionSheetAction(
                          onPressed: () {
                            bloc.reportAnthor(4);
                            Navigator.pop(context, 3);
                          },
                          child: Text(
                            '人身攻击',
                            style: TextStyle(
                              color: col("303133"),
                              fontSize: px(40),
                            ),
                          ),
                        ),
                      ],
                    );
                  },
                );
              },
              child: Image.asset(
                'assets/images/icon/report.png',
                width: px(70),
                height: px(70),
              ),
            ),
          )
        ],
      ),
    );
  }

  // 头像
  Widget headBuil() {
    return Container(
      width: px(180),
      height: px(180),
      margin: EdgeInsets.only(top: px(50)),
      child: ClipOval(
        child: CachedNetworkImage(
          imageUrl: '${widget.targetHead}',
          fit: BoxFit.cover,
          errorWidget: (context, url, error) {
            return Config.errorNetImage;
          },
        ),
      ),
    );
  }

  // 昵称
  Widget nickNameBuild() {
    return Container(
      margin: EdgeInsets.only(top: px(39)),
      child: Text(
        "${widget.targetName}",
        style: TextStyle(
          height: .99,
          color: Colors.white,
          fontSize: px(38),
        ),
      ),
    );
  }

  // 通话结束
  Widget callCloseBuild() {
    return Container(
      margin: EdgeInsets.only(top: px(50)),
      child: Text(
        "通话结束",
        style: TextStyle(
          height: .99,
          color: Colors.white,
          fontSize: px(34),
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  // 通话时长
  Widget callTimeBuild(InvitationInfoModel data) {
    int endtime = int.parse(data.endtime);
    int talktime = int.parse(data.talktime);

    return Container(
      margin: EdgeInsets.only(top: px(24)),
      child: Text(
        "通话时长：${Date.difference(talktime, endtime) ?? "00:00:00"}",
        style: TextStyle(
          height: .99,
          color: Colors.white,
          fontSize: px(28),
        ),
      ),
    );
  }

  // 直播收益
  Widget videoPriceTitleBuild(context) {
    return stateBuilder(context, 'user', (UserState user) {
      if (user.info.memberType == 1) {
        return Container(
          margin: EdgeInsets.only(top: px(145)),
          child: Text(
            "直播收益(金币)",
            style: TextStyle(
              height: .99,
              color: Colors.white,
              fontSize: px(26),
            ),
          ),
        );
      } else {
        return Container(
          margin: EdgeInsets.only(top: px(145)),
          child: Text(
            "本次消费(金币)",
            style: TextStyle(
              height: .99,
              color: Colors.white,
              fontSize: px(26),
            ),
          ),
        );
      }
    });
  }

  // 礼物
  Widget giftBuild(InvitationInfoModel data) {
    return Container(
      child: Column(
        children: <Widget>[
          Text(
            "${data.giftGlod ?? 0}",
            style: TextStyle(
              color: Colors.white,
              fontSize: px(68),
              fontWeight: FontWeight.bold,
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: px(24)),
            child: Text(
              "礼物",
              style: TextStyle(
                height: .99,
                color: Colors.white,
                fontSize: px(26),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 视频通话计费
  Widget videoPriceBuild(InvitationInfoModel data) {
    return Container(
      child: Column(
        children: <Widget>[
          Text(
            "${data.gold ?? 0}",
            style: TextStyle(
              color: Colors.white,
              fontSize: px(68),
              fontWeight: FontWeight.bold,
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: px(24)),
            child: Text(
              "视频通话",
              style: TextStyle(
                height: .99,
                color: Colors.white,
                fontSize: px(26),
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 评价主播弹窗
  void appraise(context) async {
    await Future.delayed(Duration(milliseconds: 50));
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(px(22)),
          topRight: Radius.circular(px(22)),
        ),
      ),
      builder: (BuildContext context) {
        return Container(
          height: px(800),
          child: Column(
            children: <Widget>[
              Container(
                height: px(88),
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: col("edeff2"),
                      width: 1,
                    ),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    FlatButton(
                      onPressed: () => Navigator.pop(context),
                      child: Text(
                        "取消",
                        style: TextStyle(
                          color: col("606266"),
                          fontSize: px(34),
                        ),
                      ),
                    ),
                    Text(
                      "评价主播",
                      style: TextStyle(
                        color: col("303133"),
                        fontSize: px(34),
                      ),
                    ),
                    FlatButton(
                      onPressed: () {
                        bloc.commentTag(widget.anchorId);
                        Navigator.pop(context);
                      },
                      child: Text(
                        "确定",
                        style: TextStyle(
                          color: col("5640c6"),
                          fontSize: px(34),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                height: px(712),
                child: ListView(
                  children: <Widget>[
                    Container(
                      width: w100(context),
                      padding: EdgeInsets.symmetric(
                        horizontal: px(55),
                        vertical: px(30),
                      ),
                      child: StreamBuilder(
                        stream: bloc.taglistStream,
                        builder: (
                          BuildContext context,
                          AsyncSnapshot snapshot,
                        ) {
                          if (snapshot.hasData) {
                            return Wrap(
                              spacing: px(15),
                              runSpacing: px(20),
                              children: snapshot.data.map<Widget>((item) {
                                return GestureDetector(
                                  onTapUp: (val) => bloc.choiceTag(item),
                                  child: Container(
                                    padding: EdgeInsets.symmetric(
                                      horizontal: px(22),
                                      vertical: px(12),
                                    ),
                                    decoration: BoxDecoration(
                                      color: col(
                                        "${item.xingxiangcolor.replaceAll('#', '')}",
                                      ),
                                      borderRadius: BorderRadius.circular(
                                        px(27),
                                      ),
                                    ),
                                    child: Text(
                                      "${item.usertagName}",
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: px(26),
                                      ),
                                    ),
                                  ),
                                );
                              }).toList(),
                            );
                          } else {
                            return Container();
                          }
                        },
                      ),
                    ),
                    Container(
                      alignment: Alignment.centerRight,
                      padding: EdgeInsets.symmetric(
                        horizontal: px(55),
                        vertical: px(30),
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Text(
                            "当前选择的标签",
                            style: TextStyle(
                              color: col("909399"),
                              fontSize: px(22),
                            ),
                          ),
                          Text(
                            "最多选择3个印象标签",
                            style: TextStyle(
                              color: col("909399"),
                              fontSize: px(22),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      width: w100(context),
                      padding: EdgeInsets.symmetric(
                        horizontal: px(55),
                        vertical: px(30),
                      ),
                      child: StreamBuilder(
                        stream: bloc.choiceTagStream,
                        builder: (
                          BuildContext context,
                          AsyncSnapshot snapshot,
                        ) {
                          if (snapshot.hasData) {
                            return Wrap(
                              spacing: px(15),
                              runSpacing: px(20),
                              children: bloc.choiceTagList.map<Widget>((item) {
                                return GestureDetector(
                                  onTapUp: (val) => bloc.delTag(item.id),
                                  child: Container(
                                    padding: EdgeInsets.symmetric(
                                      horizontal: px(22),
                                      vertical: px(12),
                                    ),
                                    decoration: BoxDecoration(
                                      color: col(
                                        "${item.xingxiangcolor.replaceAll('#', '')}",
                                      ),
                                      borderRadius: BorderRadius.circular(
                                        px(27),
                                      ),
                                    ),
                                    child: Text(
                                      "${item.usertagName}",
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: px(26),
                                      ),
                                    ),
                                  ),
                                );
                              }).toList(),
                            );
                          } else {
                            return Container();
                          }
                        },
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        );
      },
    );
  }
}
